#include<bits/stdc++.h>
using namespace std;
const int N = 105;
struct tlb{
	string name , gender;
	int y , m;
}s[N];
bool cmp(tlb a , tlb b){
	if(a.y != b.y){
		return a.y > b.y;
	}
	return a.m > b.m;
}
int main(){
	freopen("agesort.in", "r", stdin);
	freopen("agesort.out", "w", stdout);
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cin >> s[i].name >> s[i].gender >> s[i].y >> s[i].m;
	}
	sort(s + 1 , s + n + 1 , cmp);
	for(int i = 1 ; i <= n ; i++){
		cout << s[i].name << " " << s[i].gender << " ";
		cout << s[i].y << " " << s[i].m << endl;
	}
}
